package com.kfm.mybatis.mapper;


import com.kfm.mybatis.model.Account;
import com.kfm.mybatis.provider.AccountProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@CacheNamespace
public interface IAccountMapper {

    @Select("select * from account where uid = #{uid}")
    @Results(id = "accountMap",value = {
            @Result(id = true, property = "id", column = "id"),
            @Result( property = "uid", column = "uid"),
            @Result(property = "money", column = "money"),
    })
    List<Account> selectByUid(Integer uid);

    @Select("select * from account where id = #{id}")
    List<Account> selectByUid2(Integer id);


    @Select("select u.*, a.id aid, a.uid, a.money from user u right join account a on u.id = a.uid")
    @Results(value = {
            @Result(id = true, property = "id", column = "aid"),
            @Result(property = "uid", column = "auid"),
            @Result(property = "money", column = "money"),
            @Result(property = "user", one = @One(resultMap = "com.kfm.mybatis.mapper.IUserMapper.userMap"))
    })
    // 查所有的 账户 和 它所属的用户信息
    List<Account> selectAll();



    @Select("""
        <script>
            select * from account
            <where>
                <if test="id != null">
                    and id = #{id}
                </if>
                <if test="uid != null">
                    and uid = #{uid}
                </if>    
                <if test="money != null">
                    and money = #{money}
                </if>
            </where>
        </script>
       """)
    List<Account> selectByAccount(Account account);

    @SelectProvider(type = AccountProvider.class, method = "select")
    List<Account> selectByAccount2(Account account);

}
